<?php
namespace app\admin\controller;

use app\service\MoneyService;

class Money extends Base
{

    public $moneyModel;
    public function __construct () {
        parent::__construct();
        $this->moneyModel = new \app\common\model\Money();
    }

    /**
     * 列表
     * @return \think\response\Json
     */
    public function money_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            $where = [];
            if ($data['name']) {
                $where['b.name'] = ['like', "%{$data['name']}%"];
            }
            $list = MoneyService::S()->queryPage($where, $page, $size);
            $total = MoneyService::S()->queryCount($where);
            return returnJson(200,'',compact('page','size','list','total'));
        }
    }

    /**
     * 编辑添加
     * @return \think\response\Json
     */
    public function money_edit()
    {
        if(request()->isPost()){
            $data = input('');
            if(!$data['sid']){
                return returnJson(202,'请填写队员id');
            }
            if(!$data['pay_time']){
                return returnJson(202,'请填写缴费日期');
            }
            if(!$data['pay_money']){
                return returnJson(202,'请填写缴费金额');
            }
            $save_data = [
                'sid' => $data['sid'],
                'month' => $data['month']?$data['month']:6,
                'send_month' => $data['send_month']?$data['send_month']:0,
                'pay_time' => strtotime($data['pay_time']),
                'lose_time' => strtotime('+'.$data['month']+$data['send_month'] . 'month',strtotime($data['pay_time'])),
                'pay_money'=>$data['pay_money'],
                'update_time' => time(),
                'tuijian' => $data['tuijian']
            ];
            if($data['id']){
                $where = [
                    'id'=>$data['id']
                ];
                $res = MoneyService::S()->querySave($save_data,$where);
            }else{
                $save_data['create_time'] = time();
                $res = MoneyService::S()->querySave($save_data);
            }
            if($res){
                return returnJson(200,'编辑成功');
            }else{
                return returnJson(202,'编辑失败');
            }
        }
    }


    /**
     * 删除
     * @return \think\response\Json
     */
    public function del_money () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        $this->moneyModel->where('id',$input['id'])->delete();
        return returnJson(0,'删除成功');
    }

   
}

